import { Injectable } from '@nestjs/common'
import { Multer } from 'multer'
import { join, extname } from 'path'
import * as fs from 'fs'
@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!'
  }

  async uploadFile(file: Multer.File, targetDirectory: string) {
    const uploadPath = join(__dirname, '..', '..', targetDirectory)

    if (!fs.existsSync(uploadPath)) {
      fs.mkdirSync(uploadPath, { recursive: true })
    }
    const fileName = `${Date.now() + extname(file.originalname)}`
    const filePath = join(uploadPath, fileName)
    fs.writeFileSync(filePath, file.buffer)
    return filePath
  }
}
